import { isArray, isString } from 'lodash';

/**
 * 语言配置
 */
export const config = {
    language: 'zh-CN'
}

/**
 * 自动解析
 * @param {*} obj 
 * @param {*} arr 
 */
const autoParsing = (obj, arr) => {
    if (isString(obj) || 0 === arr.length) {
      return obj;
    }
  
    let key = arr.shift();
  
    return undefined === obj[key] ? '' : autoParsing(obj[key], arr);
}

export function $t(path) {
    const langConfig = config.language;
    const list = path.split('.');
    const objName = list.shift();
    const lang = require(`./${langConfig}/${objName}`).default;
  
    return !isArray(list) ? '' : autoParsing(lang, list);
}

